﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using RLZY.others;
using Infragistics.Win.UltraWinGrid;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
namespace RLZY
{
    public partial class FormNotKey_GDX_Add : Form
    {
        public FormNotKey_GDX_Add()
        {
            InitializeComponent();
            
        }
        //public FormBDX_Modify(string username,String 权限)
        //{
        //    InitializeComponent();
        //    this.userID= strTextBox1Text;
        //}
        private static FormNotKey_GDX_Add childFromInstanc;
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public String userID;
        public String 权限;


        public static FormNotKey_GDX_Add ChildFromInstanc
        {
            get
            {
                if (childFromInstanc == null || childFromInstanc.IsDisposed)
                //判断childFromInstanc是否有值，不为null或者窗体释放（当你将子窗体关了的时候）为childFromInstanc赋值
                {
                    childFromInstanc = new FormNotKey_GDX_Add();
                }
                return childFromInstanc;
                //返回的childFromInstanc（子窗体存在时返回的是null,关闭了（IsDisposed为true）或者第一次打开子窗体返回一个ChildForm实例）
            }
        }

        private void FormNotKey_GDX_Add_Load(object sender, EventArgs e)
        {
            try
            {
                BLL.userpassword bllpwd = new BLL.userpassword();
                Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                others.myGZ mygz = new myGZ();
                权限 = model.备注;
                this.txtBM.Text = 权限;

                //DataSet ds = mygz.GetListPart(0, 权限);
                //this.BindData(ds);
                this.BindListInfo();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void BindListInfo()
        {
            others.myGZ mygz = new myGZ();
            cmbBZ.Items.Clear();

            try
            {
                DataSet dsgz = mygz.GetListBM(权限);
                cmbBZ.DataSource = dsgz.Tables[0];
                cmbBZ.DisplayMember = "车间班组";  //列名1 
                cmbBZ.ValueMember = "车间班组";    //列名2 
                
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
            }
        }

        private void BindData(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            //bindingSource1.DataSource = dv;
            this.ultraGrid1.DataSource = dv;
        }
        //列选择器
        private void btnCustomColumnChooser_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowCustomColumnChooserDialog();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        private RLZY.CustomColumnChooser customColumnChooserDialog = null;
        
        private void ShowCustomColumnChooserDialog()
        {
            if (null == this.customColumnChooserDialog || this.customColumnChooserDialog.IsDisposed)
            {
                this.customColumnChooserDialog = new RLZY.CustomColumnChooser();
                this.customColumnChooserDialog.Owner = this;
                this.customColumnChooserDialog.Grid = this.ultraGrid1;
            }

            this.customColumnChooserDialog.Show();
        }
        //修改列布局
        private void btnDisplayLayout_Click(object sender, EventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
        }
        //隐藏列
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            e.Layout.Bands[0].Columns[0].Hidden = true;
        }

        private void cmbBZ_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                BLL.userpassword bllpwd = new BLL.userpassword();
                Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                others.NotKey notkey = new NotKey();
                权限 = model.备注;

                DataSet ds = notkey.GetListNotKey(权限, this.cmbBZ.Text.Trim());
                this.BindData(ds);
                
            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        int id;

        private void ultraGrid1_DoubleClickCell(object sender, DoubleClickCellEventArgs e)
        {
            try
            {
                int i = this.ultraGrid1.ActiveRow.Index;
                id = Convert.ToInt32(this.ultraGrid1.Rows[i].Cells[0].Value.ToString().Trim());
                this.dtpDate.Text = this.ultraGrid1.Rows[i].Cells[1].Value.ToString().Trim();
                this.Dat = this.ultraGrid1.Rows[i].Cells[1].Value.ToString().Trim();
                this.txtName.Text = this.ultraGrid1.Rows[i].Cells[5].Value.ToString().Trim();
                this.txtBM.Text = this.ultraGrid1.Rows[i].Cells[2].Value.ToString().Trim();
                this.cmbBZ.Text = this.ultraGrid1.Rows[i].Cells[3].Value.ToString().Trim();
                this.txtGW.Text = this.ultraGrid1.Rows[i].Cells[3].Value.ToString().Trim();
                this.txtNameID.Text = this.ultraGrid1.Rows[i].Cells[24].Value.ToString().Trim();
                this.txtZH.Text = this.ultraGrid1.Rows[i].Cells[25].Value.ToString().Trim();

                this.txtPlace.Text = this.ultraGrid1.Rows[i].Cells[35].Value.ToString().Trim();
            }
            catch
            {
                //Common.RecordCommLog(ex);
                return;
            }

        }
        public void Last_Date()
        {
            others.myGZ mygz = new myGZ();
            DataSet dataSet = mygz.GetListLastDate(权限);
           
            bool flag = dataSet.Tables[0].Rows.Count > 0;
            if (flag)
            {
                this.LastDate = dataSet.Tables[0].Rows[0]["日期"].ToString();
            }
            else
            {
                this.LastDate = "9998-12-31";
            }
            
        }
        public void Last_DateNotLimited()
        {
            others.myGZ mygz = new myGZ();
            DataSet dataSet = mygz.GetListLastDateNOtLimited(权限);

            bool flag = dataSet.Tables[0].Rows.Count > 0;
            if (flag)
            {
               this.dtpDate.Text = this.VarDat =dataSet.Tables[0].Rows[0]["日期"].ToString().Trim ();
                //this.LastDate = dataSet.Tables[0].Rows[0]["日期"].ToString().Trim ();
            }
            else
            {
                this.VarDat = "";
                this.dtpDate.Text = "";
            }

        }
        public string LastDate { get; set; }

        
        public int GZAdd()
        {
            Model.GZ model = new Model.GZ();
            model.日期 = this.VarDat.Trim().ToUpper();
            model.部门单位 = this.txtBM.Text.Trim();
            model.车间班组 = this.cmbBZ.Text;
            model.姓名 = this.txtName.Text.Trim();
            model.岗位名称 = this.txtGW.Text.Trim();
            model.身份证号 = this.txtNameID.Text.Trim();
            model.帐号 = this.txtZH.Text.Trim();
            model.人数 = 1;
            model.是否保护 = "否";
            model.籍贯_出生地 = this.txtPlace.Text.Trim();
            BLL.GZ bll = new BLL.GZ();
            return bll.Add(model);

        }
        

        private void GZ_BZ_Modify()
        {
            others.myGZ mygz = new myGZ();
            BLL.GZ bll = new BLL.GZ();

            int count = mygz.set0TONull();
            //if (count <= 0) { return; }
           
            foreach (UltraGridRow row in this.ultraGrid1.Rows)
            {
                int rowid = Convert.ToInt32(row.Cells["id"].ToString().Trim());
               
            }
                
            
        }
        private void GZ_BZ_ModifyOne()
        {
            others.myGZ mygz = new myGZ();
            BLL.GZ bll = new BLL.GZ();

            
            int count = mygz.set0TONull();
          
        }
       

        public string Dat { get; set; }

        
       
        
        //按回车键跳转

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter && !(ActiveControl is Button))
            {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

        private void btnOne_Click(object sender, EventArgs e)
        {
            try
            {
                string strErr = "";
                if (this.cmbBZ.Text.Trim().Length == 0)
                {
                    strErr += "班组不能为空！\r\n";
                }
                if (this.txtName.Text.Trim().Length == 0)
                {
                    strErr += "姓名不能为空！\r\n";
                }
                if (this.txtNameID.Text.Trim().Length == 0)
                {
                    strErr += "身份证号不能为空！\r\n";
                }
                if (NameIDIS_Exist())
                {
                    strErr += "身份证号码重复！\r\n";
                }
                if (this.ZHIS_Exist())
                {
                    strErr += "账号重复！\r\n";
                }

                this.Last_Date();
                this.Last_DateNotLimited();
                DateTime time1 = DateTime.Parse(this.VarDat);
                DateTime time2 = DateTime.Parse(this.LastDate);
                
                if( DateTime.Compare(time1, time2) < 0)
                {
                    strErr += "已做保护！\r\n";
                }
                if (strErr != "")
                {
                    MessageBox.Show(this, strErr);
                    return;
                }
                    this.btnAdd.Enabled = false;
                    
                    if (this.GZAdd() > 0)
                    {
                        Msg.DisplayMsg("提示", "新增成功。\r\n本提示窗口2秒钟后自动消失", 1000);
                    }
                    BLL.userpassword bllpwd = new BLL.userpassword();
                    Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                    others.NotKey notkey = new NotKey();
                    权限 = model.备注;

                    DataSet ds = notkey.GetListNotKey(权限, this.cmbBZ.Text.Trim());
                    this.BindData(ds);
                    this.btnAdd.Enabled = true;

                
            }
            catch (Exception ex) { LOG.Error(ex); }
            
        }
        public bool NameIDIS_Exist()
        {
            others.NotKey not = new NotKey();
            SqlDataReader re = not.GetListNameId(this.txtNameID.Text.Trim());
            if (re.Read ())
            {
                return true;
            }
            else
            {
                return false;
            }
            
        }
        public bool ZHIS_Exist()
        {
            others.NotKey not = new NotKey();
            SqlDataReader re = not.GetListZH(this.txtZH.Text.Trim());
            if (re.Read())
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public string VarDat { get; set; }
    }
}
